<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@include file='templates/header.jsp'%>
<%@ page import="awesome.Manager.ItemManager" %>
<%@ page import="awesome.Manager.Item" %>
<%@ page import="java.util.LinkedList" %>
<%@ page import="java.util.ArrayList" %>
<script type="text/javascript" src="js/jscalendar-1.0/calendar.js"></script>
<script type="text/javascript" src="js/jscalendar-1.0/lang/calendar-en.js"></script>
<link rel="stylesheet" type="text/css" media="all" href="js/jscalendar-1.0/calendar-blue2.css" title="blue" />
<%@include file='checkLoggedIn.jsp'%>

<script type="text/javascript">

//This function gets called when the end-user clicks on some date.
function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  if (cal.dateClicked ){
    cal.callCloseHandler();
  }
}

function closeHandler(cal) {
	cal.hide();                        // hide the calendar
	cal.destroy();
	_dynarch_popupCalendar = null;
}

function showCalendar(id, format) {
	 var el = document.getElementById(id).getElementsByTagName("input");
	  if (_dynarch_popupCalendar != null) {
	    // we already have some calendar created
	    _dynarch_popupCalendar.hide();                 // so we hide it first.
	  } else {
		  
	    var cal = new Calendar(1, null, selected, closeHandler);
	    cal.weekNumbers = false;
	    cal.showsTime = true;
	    cal.time24 = true;
	    cal.showsOtherMonths = true;
	  
	    _dynarch_popupCalendar = cal;                  // remember it in the global var
	    cal.setRange(1900, 2070);        			  // min/max year allowed.
	    cal.create();
	  }
	  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
	  _dynarch_popupCalendar.parseDate(el[0].value);      // try to parse the text in field
	  _dynarch_popupCalendar.sel = el[0];                 // inform it what input field we use
	  
	  _dynarch_popupCalendar.showAtElement(el[1], "Br");        // show the calendar

				
	  return false;
}
</script>
<h1>Create an Auction</h1>
<% 
	int uid = Integer.parseInt(session.getAttribute("uid").toString());
	ItemManager im = new ItemManager();
	String function = request.getParameter("func");
	//System.out.println(function);
	
	if(function != null){		
		if(function.equals("create_auction")){
			
			String errors = "";
			String itemName = request.getParameter("name");
			String description = request.getParameter("description");
			String start = request.getParameter("start_date");
			String end = request.getParameter("end_date");
			String category = request.getParameter("category");
			Double minBid = 0.0;
			
			try{
				minBid = Double.valueOf(request.getParameter("min_bid"));
			}catch(NumberFormatException e){
				errors += "invalid minimum bid<br/>";
			}
			if(itemName == null || itemName.equals("")){
				errors += "invalid name<br/>";
			}
			if(description == null || description.equals("")){
				errors += "invalid description<br/>";
			}
			if(start == null || start.equals("")){
				errors += "invalid start time<br/>";
			}	
			if(end == null || end.equals("")){
				errors += "invalid end time<br/>";
			}	
			
			if(errors.equals("")){ // no errors
				im.insertItem(uid,Integer.valueOf(category),description,start,end,minBid,itemName);
				LinkedList<Item> items = im.getItemsForUser(uid);
				int iid = items.get(items.size()-1).getItemID();
				session.setAttribute("iid",iid);
				response.sendRedirect("pic.jsp");
			}else{ // display error message
				response.sendRedirect("create_auction.jsp?func=input_error&error_message="+errors);
			}			
		}else if(function.equals("input_error")){
			String errorMessage = request.getParameter("error_message");
			if(errorMessage == null){
				errorMessage = "Unknown Error!";
			}
			out.write("<div style='color:rgb(255,0,0);'>INPUT ERROR:<br/> "+errorMessage+"</div");
		}
	}
%>
<form action="create_auction.jsp" method="POST">
	<input type="hidden" name="func" value="create_auction"/>
	<table align="left">
		<tr>
			<td>Item Name:</td>
			<td><input type="text" name="name"/></td>
			<td>
			Select Category:
				<select name="category">
					<%
						LinkedList<String> categories = im.getCategories();
						for(int i = 0; i < categories.size(); i++){
							out.write("<option value='"+i+"'>"+categories.get(i)+"</option>");
						}
					%>
				</select>
			</td>			
		</tr>
		<tr>
			<td>Minimum Bid:</td>
			<td><input type="text" name="min_bid"/></td>
		</tr>
		<tr id="start">
			<td>Start Date:</td>
			<td><input type="text"  name="start_date"/>
			<input type="reset" value=" ... " onclick="return showCalendar('start', '%Y-%m-%d %H:%M:%S');"></td>
		</tr>
		<tr id="end">
			<td>End Date:</td>
			<td><input type="text" name="end_date"/>
			<input type="reset" value=" ... " onclick="return showCalendar('end', '%Y-%m-%d %H:%M:%S');"></td>
		</tr>
		<tr>
			<td valign="top">Description:</td>
			<td colspan="2"><textarea name="description"rows="10" cols="50"></textarea></td>
		</tr>
		<tr>
			<td><input type="submit" value="Create Auction"/></td>
		</tr>
	</table>	
</form>
<%@include file='templates/footer.jsp'%>